<?php
class admin_contentController extends adminController{


    // 内容列表
    public function index()
    {
        $this->model_info=model('admin_category')->get_model();
        $this->view()->assign(api('admin','content_common_list'));
        $this->show();
    }

    //AJAX获取内容列表
    public function ajax_content_list()
    {
        //分页参数
        $listRows=20;
        $page = intval($_POST['page']);
        $limit_start = ($page - 1) * $listRows;
        $limit = $limit_start . ',' . $listRows;

        //获取公共信息条件
        $where=api('admin','content_common_list_where');

        //列表信息
        $list=model('admin_content')->content_list($limit,$where['where'],$where['order']);
        $count=model('admin_content')->count($where['where']);

        //总页数
        $countpage=ceil($count/$listRows);

        if($list){
            foreach ($list as $vo) {
                $html.='
                <tr id="del_'.$vo['aid'].'">
                    <td><input name="id[]" type="checkbox" id="id[]" value="'.$vo['aid'].'" ></td>
                    <td>'.$vo['aid'].'</td>
                    <td><span>'.$vo['title'];
                if(!empty($vo['image'])){
                    $html.=' <a href="javascript:void(0);" rel="'.$vo['image'].'" class="images"><img align="AbsMiddle" src="'.__PUBLIC__.'/images/ico/pic.png" width="14" height="14" alt="" /></a>';
                }
                if(!empty($vo['recommend'])){
                    $str_list=api('recommend','content_list',$vo['recommend']);
                    foreach ($str_list as $value) {
                        $html.=' <font color=green>['.$value['name'].']</font>';
                    }
                }
                $html.='</span>
                    </td>
                    <td>'.$vo['cname'].'</td>
                    <td>';
                if($vo['status']>0){
                    $html.='<font color=green><b>√</b></font>';
                }else{
                    $html.='<font color=red><b>×</b></font>';
                }
                $html.='
                    </td>
                    <td>';
                $html.=date('Y-m-d H:i:s',$vo['updatetime']);
                $html.='</td>
                    <td>
                    <a class="opentab operating" href="javascript:void(0);"  title="编辑'.$vo['name'].'内容" url="'.url('admin_content/edit',array('aid' => $vo['aid'])).'" ><span class="icon">&#xf040;</span> 修改</a>
                    <a class="operating" href="javascript:void(0);" onClick="del(\''.url('admin_content/del').'\',\''.$vo['aid'].'\',this)"><span class="icon">&#xf014;</span> 删除</a>
                    </td>
                </tr>
                ';
            }
            $status=1;
        }else{
            $html='<tr><td colspan="7"><center>暂无查找到内容<center></td></tr>';
            $status=0;
        }

        @header("Content-type:text/html");
        echo json_encode(array('status' => $status, 'info' => $html ,'countpage'=>$countpage));
    }
    
    public function common_data_info()
    {
        $model_info=model('admin_category')->get_model();
        if(!empty($model_info['content_befrom'])){
            $befrom=explode("\n",$model_info['content_befrom']);
            foreach ($befrom as $value) {
                $befrom_list[]=$value;
            }
        }
        $data['model_info']=$model_info;
        $data['befrom_list']=$befrom_list;
        return $data;
    }

    //内容添加
    public function add()
    {
        $this->view()->assign(api('admin','content_common_info'));
        $this->view()->assign($this->common_data_info());
        $this->show('admin_content/info');
    }

    //内容保存
    public function add_save()
    {
        api('admin','content_common_data_check',$_POST);
        //保存内容信息
        $_POST['aid']=api('admin','content_add',$_POST);
        model('admin_content')->add_save($_POST);
        $this->msg('内容添加成功！',1);
    }

    //内容编辑
    public function edit()
    {
        $aid=intval($_GET['aid']);
        $this->alert_str($aid,'int');
        $this->view()->assign(api('admin','content_common_info',$aid));
        $this->view()->assign($this->common_data_info());
        //获取附加数据
        $this->page_info=model('admin_content')->info($aid);
        $this->show('admin_content/info');
    }

    //内容保存
    public function edit_save()
    {
        api('admin','content_common_data_check',$_POST);
        //保存内容信息
        api('admin','content_edit',$_POST);
        model('admin_content')->edit_save($_POST);
        $this->msg('内容编辑成功！',1);
    }

    //内容删除
    public function del()
    {
        $aid=intval($_POST['aid']);
        $this->alert_str($aid,'int',true);
        api('admin','content_del',$aid);
        model('admin_content')->del($aid);
        $this->msg('内容删除成功！',1);
    }

    //批量操作
    public function batch(){
        if(empty($_POST['status'])||empty($_POST['id'])){
            $this->msg('操作无效！',0);
        }
        $id_array=substr($_POST['id'],0,-1);
        $id_array=explode(',', $id_array);
        if(empty($id_array)){
            $this->msg('请先选择内容！',0);
        }
        switch ($_POST['status']) {
            case '1':
                //审核
                foreach ($id_array as $value) {
                    api('admin','content_status',array('aid'=>$value,'status'=>1));
                }
                break;
            case '2':
                //草稿
                foreach ($id_array as $value) {
                    api('admin','content_status',array('aid'=>$value,'status'=>0));
                }
                break;
            case '3':
                //删除
                foreach ($id_array as $value) {
                    api('admin','content_del',$value);
                    model('admin_content')->del($value);
                }
                break;
            case '4':
                //转移栏目
                $cid=intval($_POST['cid']);
                if(empty($cid)){
                    $this->msg('请先选择目标栏目！',0);
                }
                foreach ($id_array as $value) {
                    api('admin','content_edit_cid',array('aid'=>$value,'cid'=>$cid));
                }
                break;
        }
        $this->msg('操作执行完毕！',1);

    }

    




}

?>